﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using log4net;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for VideoAssignmentPage.xaml
    /// </summary>
    public partial class VideoAssignmentPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(VideoAssignmentPage));
        private readonly CoreConfig Config;
        private readonly PageConfig pConfig;
        private readonly WorkflowCompleter _workflow;

        public VideoAssignmentPage(PageConfig pageConfig, bool landscape = false) {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;
            pConfig = pageConfig;
            PreviewRoot.Content = PagePreviewFactory.Create(pConfig);
            _workflow = new WorkflowCompleter(this);

            //If the video is supposed to be in landscape mode, rotate the iPad to give a better preview
            if(landscape) {
                PreviewRoot.LayoutTransform = new RotateTransform(90);
            }

            //Construct thumbnails from all of the presentation slides
            Slides = new Collection<PowerPoint.Slide>();

            foreach(PowerPoint.Slide s in Config.Presentation.Slides) {
                Slides.Add(s);
            }

            
            if(Slides.Count > 0) {
                CurrentSlideIndex = 0;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public PowerPoint.Slide _currentPreviewSlide;
        public PowerPoint.Slide CurrentPreviewSlide {
            get {
                return _currentPreviewSlide;
            }
            set {
                _currentPreviewSlide = value;
                NotifyPropertyChanged("CurrentPreviewSlide");
            }
        }

        private void DisplaySlide(PowerPoint.Slide slide) {
            try {

                SlidePreview.Source = SlideUtil.GetPreview(slide);
                CurrentPreviewSlide = slide;

                PowerPointSlideNumberText.Text = string.Format("PowerPoint Slide {0}", slide.SlideNumber);

            } catch(Exception ex) {
                log.Warn("Cannot export slide", ex);
            }
        }

        private Collection<PowerPoint.Slide> Slides;
        private int CurrentSlideIndex;

        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }

        private void PrevSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex > 0) {
                CurrentSlideIndex = CurrentSlideIndex - 1;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        private void NextSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex < Slides.Count - 1) {
                CurrentSlideIndex = CurrentSlideIndex + 1;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        private void UpdateButtons() {
            NextSlideButton.IsEnabled = (CurrentSlideIndex < Slides.Count - 1);
            PrevSlideButton.IsEnabled = (CurrentSlideIndex > 0);
        }

        private void ChooseSectionButton_Click(object sender, RoutedEventArgs e) {
            //Add this config to the presentation at the given location
            PowerPoint.Slide slideToAssign = CurrentPreviewSlide;

            SlideConfig newSlideConfig = new SlideConfig(slideToAssign.SlideID);
            PageConfig defaultPage = newSlideConfig.Pages.First();
            newSlideConfig.ReplacePage(defaultPage, pConfig);
            Config[slideToAssign.SlideID] = newSlideConfig;

            //Show dialog to prompt additional navigation
            ContinueDialog.Visibility = Visibility.Visible;
            _workflow.Complete();
        }

        private void DialogNext_Clicked(object sender, EventArgs e) {
            //Continue on to review and publish
            ContinueDialog.Visibility = Visibility.Collapsed;
            NavigationService navService = this.NavigationService;
            if(navService != null) {
                navService.Navigate(new ReviewPage());
            }
        }

        private void DialogAlternate_Clicked(object sender, EventArgs e) {
            //Go to add another video
            ContinueDialog.Visibility = Visibility.Collapsed;
            NavigationService navService = this.NavigationService;
            if(navService != null) {
                navService.Navigate(new SelectVideoPage());
            }
        }

        
    }
}
